//
//  BottomSelectionBoxView.m
//  Toyota
//
//  Created by ld on 2024/6/26.
//

#import "BottomSelectionBoxView.h"
#import "Definition.h"
#import "Helper.h"
@implementation BottomSelectionBoxView
-(instancetype)initWithFrame:(CGRect)frame andTitleStr:(NSString *)titleStr andMsgStr:(NSString *)msgStr andCellArray:(NSArray*)cellArray andIsModel:(BOOL)isModel andChoiceArr:(NSArray *)choiceArr hasMulti:(BOOL)multi {
    self = [super initWithFrame:frame];
    if (self) {
        [self viewAddSubView:frame andTitleStr:titleStr andMsgStr:msgStr andCellArray:cellArray andIsModel:isModel andChoiceArr:choiceArr hasMulti:multi];
    }
    return self;
}


- (void)viewAddSubView:(CGRect)frame andTitleStr:(NSString *)titleStr andMsgStr:(NSString *)msgStr andCellArray:(NSArray*)cellArray andIsModel:(BOOL)isModel andChoiceArr:(NSArray *)choiceArr hasMulti:(BOOL)multi {
    _cellArr = cellArray;
    _isModel = isModel;
    _multi = multi;
    _choiceArr =[NSMutableArray arrayWithArray:choiceArr];
    
    _bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.customWidth, self.customHeight)];
    _bgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_bgView];
    
    UILabel * titleLab = [[UILabel alloc]initWithFrame:CGRectMake(40, 25, self.customWidth - 80., 20)];
    titleLab.text = titleStr;
    titleLab.textAlignment = NSTextAlignmentCenter;
    titleLab.textColor = kColor141414;
    titleLab.font = fontBold(17);
    [_bgView addSubview:titleLab];
    
    UIButton * closeBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    closeBtn.frame = CGRectMake(self.customWidth - 35, 20, 18, 18);
    [closeBtn setImage:[UIImage imageNamed:@"nav_close"] forState:(UIControlStateNormal)];
    [closeBtn addTarget:self action:@selector(closeBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self addSubview:closeBtn];
    
    float viewH = titleLab.customBottom;
    if (msgStr.length > 0) {
        UILabel * msgLab = [[UILabel alloc]initWithFrame:CGRectMake(20, viewH + 18, self.customWidth - 40., 20)];
        msgLab.text = msgStr;
        msgLab.textAlignment = NSTextAlignmentCenter;
        msgLab.textColor = kColor969696;
        msgLab.font = fontSize(13);
        [_bgView addSubview:msgLab];
        viewH = msgLab.customBottom;
    }
    
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, viewH, self.customWidth, _bgView.customHeight - 50 - CYSafeAreaBottom - viewH)];
    _scrollView.bounces = NO;
    _scrollView.backgroundColor = [UIColor whiteColor];
    [_bgView addSubview:_scrollView];
    
    float btnBottom = 0;
    for (int i = 0; i < cellArray.count; i++) {
        UIButton * cellBtn = [UIButton initWithButton:CGRectMake(25, 10 + 47 * i, self.customWidth - 50, 47) text:cellArray[i] font:fontSize(16) textColor:kColor141414 selectedTextColor:kColorE93F33 normalImg:[UIImage imageNamed:@"choice_img_nomal"] highImg:[UIImage imageNamed:@""] selectedImg:[UIImage imageNamed:@"choice_img_red"]];
        if (choiceArr.count) {
            for (NSString * choiceStr in choiceArr) {
                if ([cellArray[i] isEqualToString:choiceStr]) {
                    cellBtn.selected = YES;
                }
            }
        }
        
        cellBtn.tag = 1000 + i;
        [cellBtn borderViewForColor:kColorEDEDED borderWidth:1 borderType:(UIBorderSideTypeBottom)];
        if (multi) {
            [cellBtn addTarget:self action:@selector(cellBtnMultiAction:) forControlEvents:(UIControlEventTouchUpInside)];
        }else {
            [cellBtn addTarget:self action:@selector(cellBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
        }
        cellBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [cellBtn setTranslatesAutoresizingMaskIntoConstraints:NO];
        // 设置内间距，确保图标和文字的位置
        [cellBtn setContentEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
        [cellBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, -cellBtn.imageView.image.size.width, 0, cellBtn.imageView.image.size.width)];
        [cellBtn setImageEdgeInsets:UIEdgeInsetsMake(0, self.customWidth - 34 - cellBtn.titleLabel.intrinsicContentSize.width, 0, 0)];
        [_scrollView addSubview:cellBtn];
        btnBottom = cellBtn.customBottom + 12;
    }
   
    _scrollView.frame = CGRectMake(0, viewH, self.customWidth, _bgView.customHeight - 50 - CYSafeAreaBottom - viewH);
    _scrollView.contentSize = CGSizeMake(self.customWidth, btnBottom + 15);
    
    UIButton * saveBtn = [UIButton initWithButton:CGRectMake(20, _scrollView.customBottom + 10, self.customWidth - 40, 42) text:@"保存" font:fontSize(15) textColor:kColor141414 selectedTextColor:kColor141414 normalImg:[UIImage imageNamed:@""] highImg:[UIImage imageNamed:@""] selectedImg:[UIImage imageNamed:@""]];
    
    saveBtn.backgroundColor = kColor141414;
    [saveBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [saveBtn rounded:20];
    [saveBtn addTarget:self action:@selector(saveBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [_bgView addSubview:saveBtn];
    
}

- (void)closeBtnAction:(UIButton *)sender {
    if (self.closeBtnClick) {
        self.closeBtnClick();
    }
}

//单选
- (void)cellBtnAction:(UIButton *)sender {
    for (int i = 0; i < _cellArr.count; i++) {
        UIButton * btn = [_scrollView viewWithTag:1000 + i];
        btn.selected = NO;
    }
    sender.selected = YES;
    
    _choiceArr = [NSMutableArray arrayWithArray:@[sender.titleLabel.text]];
   
}

//多选
- (void)cellBtnMultiAction:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected) {
        [_choiceArr addObject:sender.titleLabel.text];
    }else {
        [_choiceArr removeObject:sender.titleLabel.text];
    }
   
}

- (void)saveBtnAction:(UIButton *)sender {
    if (_multi) {
        if (self.choiceMultiBtnClick) {
            self.choiceMultiBtnClick(_choiceArr);
        }
    }else {
        if (self.choiceBtnClick) {
            self.choiceBtnClick(_choiceArr.firstObject);
        }
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
